package com.tgjorgoski.sidekicknotes.browser;

import java.awt.Color;
import java.awt.Cursor;
import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;


import com.tgjorgoski.sidekicknotes.filedata.FileInformation;
import com.tgjorgoski.sidekicknotes.htmlview.HTMLDocChanged;
import com.tgjorgoski.sidekicknotes.htmlview.HTMLEditorKitChanged;
import com.tgjorgoski.utils.FileMethods;
import com.tgjorgoski.window.Session;

public class SearchBrowserSubPanel extends JTextPane implements
		AbstractFileBrowserPanel, HyperlinkListener {

	HTMLEditorKit htmlEditorKit;
	List<File> filesShown = new ArrayList<File>();

	public SearchBrowserSubPanel() {
		setBackground(new Color(240,240,240));
		htmlEditorKit = new HTMLEditorKitChanged();
		// htmlEditorKit.setDefaultCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
		StyleSheet css = htmlEditorKit.getStyleSheet();
		String styleChanges = "p { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }"
				+ "body { font-size:12pt; font-family:Arial; margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }"
				+ "html { margin-top: 5; margin-bottom: 5; margin-left:5;}"
				+ " a {font-size:14pt}";
		css.addRule(styleChanges);

		setEditorKit(htmlEditorKit);
		setEditable(false);
		addHyperlinkListener(this);
	}

	public boolean focusOnFile(File file) {
		//if (filesShown.indexOf(file)!=-1) return true;
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isVertical() {
		return true;
	}
	
	public void refreshView() {
	}

	public void setFileList(List<File> files, List<String> snippets) {
		filesShown.clear();
		filesShown.addAll(files);
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (File file : files) {
			String snippet = null;
			if (snippets != null) {
				snippet = snippets.get(i);
			}
			i++;
			String name = file.getName();
			if (name.endsWith(".html")) name=name.substring(0, name.length()-5);

			sb.append("<a href=\""
					+ FileMethods.getNormPath(file)
					+ "\">" + name + "</a>");
			if (snippet != null) {
				sb.append("<br/>");
				sb.append(snippet);
			}
			FileInformation fi = FileInformation.getFileInformation(file);
			if (fi.getLastModified()!=null) {
				sb.append("(" + fi.getLastModified() + ")");
			}
			sb.append("<br/><br/>");
		}
		setHTML(sb.toString());
		setCaretPosition(0);
	}

	private void setHTML(String html) {
		HTMLDocChanged htdoc = (HTMLDocChanged) htmlEditorKit
				.createDefaultDocument();
		setDocument(htdoc);
		try {
			StringReader reader = new StringReader(html);
			htmlEditorKit.read(reader, htdoc, htdoc.getLength());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void focusOnFirstFile() {
		// TODO Auto-generated method stub

	}

	public JComponent getViewportView() {
		return this;
	}

	BrowserPanel bPanel;

	public BrowserPanel getBrowserPanel() {
		return bPanel;
	}

	public void setBrowserPanel(BrowserPanel browserPanel) {
		this.bPanel = browserPanel;
	}

	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			String desc = e.getDescription();
			File file = new File(Session.getHomeFolder(), desc);
			Session.getHTMLView().requestFileOpen(file);
		} else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
			setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		}
	}

}
